﻿using DevExpress.XtraBars.Docking;
using SuperMap.Data;
using SuperMap.Sample.Data;
using SuperMap.UI;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace Rail.Start
{
    public partial class FormMain : DevExpress.XtraBars.Ribbon.RibbonForm, Base.IFormMain
    {
        public static FormMain formMain;
        public SampleRun m_sampleRun;
        StatusStrip m_StatusStrip;
        ToolStripStatusLabel m_StatusLabel;
        ToolStripProgressBar m_ProgressBar;
        Workspace m_workspace = new Workspace();

        public FormMain()
        {
            InitializeComponent();
            CreaateStatusBar();
            this.RibbonControl.ApplicationIcon = (Bitmap)Image.FromFile(Commons.Paths.ApplicationResourcesDir + "\\RailIcon.png");
            formMain = this;
           
            m_sampleRun = new SampleRun(m_workspace);
            m_sampleRun.OnCheck += new SampleRun.CheckEventHandler(m_sampleRun_OnCheck);
            m_sampleRun_OnCheck(true);
        }

        /// <summary>
        /// 底部的DockPanel
        /// </summary>
        public DevExpress.XtraBars.Docking.DockPanel DockPanelBottom
        {
            get
            {
                return m_DockPanelMessage;
            }
        }
        /// <summary>
        /// 中间区域场景所在的DockPanel
        /// </summary>
        public DevExpress.XtraBars.Docking.DockPanel DockPanelContainerScene
        {
            get
            {
                return m_DockPanelContainerScene;
            }
        }
        /// <summary>
        /// 窗体的Ribbon控件
        /// </summary>
        public DevExpress.XtraBars.Ribbon.RibbonControl RibbonControl
        {
            get
            {
                return m_RibbonControl;
            }
        }
        /// <summary>
        /// 空间管理器树控件
        /// </summary>
        public System.Windows.Forms.TreeView WorkspaceTree
        {
            get
            {
                return m_WorkspaceTree;
            }
        }
        public ToolStripStatusLabel StatusLabel { get => m_StatusLabel; set => m_StatusLabel = value; }
        public ToolStripProgressBar ProgressBar { get => m_ProgressBar; set => m_ProgressBar = value; }


        /// <summary>
        /// 拦截退出事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.UserClosing)
            {
                e.Cancel = false;
                return;
            }
            if (DevExpress.XtraEditors.XtraMessageBox.Show("          请确认是否直接退出系统 ?          ", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// DockManager对象
        /// </summary>
        public DockManager DockManager()
        {
            return this.m_DockManager;
        }

        /// <summary>
        /// 工具箱面板
        /// </summary>
        /// <returns>左侧停靠面板</returns>
        public DockPanel LeftDockPanel()
        {
            return this.m_DockPanelTool;
        }

        /// <summary>
        /// 工程数据面板
        /// </summary>
        /// <returns>右侧停靠面板</returns>
        public DockPanel RightDockPanel()
        {
            return this.m_DockPanelWorkspace;
        }  
        /// <summary>
        /// 图层管理器面板
        /// </summary>
        /// <returns>右侧停靠面板</returns>
        public DockPanel LayerDockPanel()
        {
            return this.m_DockPanelLayer;
        }
        /// <summary>
        /// 图层管理器
        /// </summary>
        public LayersControl LayersControlpanel
        {
            get
            {
                return this.m_LayersControl;
            }
        }
        /// <summary>
        /// 消息面板
        /// </summary>
        /// <returns>底部停靠面板</returns>
        public DockPanel BottomDockPanel()
        {
            return this.m_DockPanelMessage;
        }

        private void CreaateStatusBar()
        {
            this.m_StatusStrip = new System.Windows.Forms.StatusStrip();
            m_StatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
            m_ProgressBar = new System.Windows.Forms.ToolStripProgressBar();
            // 
            // toolStripStatusLabel1
            // 
            m_StatusLabel.Name = "toolStripStatusLabel1";
            m_StatusLabel.Size = new System.Drawing.Size(56, 17);
            m_StatusLabel.Text = "准备就绪";
            // 
            // m_ProgressBar
            // 
            m_ProgressBar.Name = "m_ProgressBar";
            m_ProgressBar.Visible = false;
            m_ProgressBar.Size = new System.Drawing.Size(100, 16);
            // 
            // m_StatusStrip
            // 
            this.m_StatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            m_StatusLabel,
            ProgressBar});
            this.m_StatusStrip.Location = new System.Drawing.Point(0, 571);
            this.m_StatusStrip.Name = "m_StatusStrip";
            this.m_StatusStrip.Size = new System.Drawing.Size(987, 22);
            this.m_StatusStrip.TabIndex = 5;
            this.m_StatusStrip.Text = "statusStrip1";
            this.Controls.Add(this.m_StatusStrip);

            LayersControl layersControl = new LayersControl();
            layersControl.Dock = DockStyle.Fill;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 工程数据 面板 DockPanel 控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barToggleSwitchItem3_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (m_DockPanelWorkspace.Visibility == DockVisibility.Hidden)
            {
                this.m_DockPanelWorkspace.Show();
            }
            else
            {
                this.m_DockPanelWorkspace.Visibility = DockVisibility.Hidden;
            }
        }

        /// <summary>
        /// 工具箱 面板 DockPanel 控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barToggleSwitchItem2_CheckedChanged_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (m_DockPanelTool.Visibility == DockVisibility.Hidden)
            {
                this.m_DockPanelTool.Show();
            }
            else
            {
                this.m_DockPanelTool.Visibility = DockVisibility.Hidden;
            }
        }
        /// <summary>
        /// 消息窗口 面板 DockPanel 控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_BarToggleItemMessageWin_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (m_DockPanelMessage.Visibility == DockVisibility.Hidden)
            {
                this.m_DockPanelMessage.Show();
            }
            else
            {
                this.m_DockPanelMessage.Visibility = DockVisibility.Hidden;
            }
        }
        /// <summary>
        /// 全屏
        /// </summary>
        public DevExpress.XtraBars.BarButtonItem BarButtonItemFullScreen
        {
            get
            {
                return this.m_BarButtonItemFullScreen;
            }
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        public DevExpress.XtraBars.BarButtonItem BarBtnItemExit
        {
            get
            {
                return this.m_BarBtnItemExit;
            }
        }

        /// <summary>
        /// 关闭工程
        /// </summary>
        public DevExpress.XtraBars.BarButtonItem BarButtonItemProjectClose
        {
            get
            {
                return this.m_BarButtonItemProjectClose;
            }
        }
        /// <summary>
        /// 保存工程按钮
        /// </summary>
        public DevExpress.XtraBars.BarButtonItem BarButtonItemProjectStore
        {
            get
            {
                return this.m_BarButtonItemProjectStore;
            }
        }

        /// <summary>
        /// 做操作的同时设置界面
        /// Initialize the interface when do the operation
        /// </summary>
        /// <param name="args"></param>
        private void m_sampleRun_OnCheck(Boolean isInitialize)
        {
            try
            {
                if (isInitialize)
                {
                    BarButtonItemProjectStore.Enabled = false;  //保存
                    m_BarButtonItemProjectSaveAs.Enabled = false;   //另存
                    m_BarButtonItemProjectClose .Enabled = false;   //打开
                }
                else
                {
                    BarButtonItemProjectStore.Enabled = true;
                    m_BarButtonItemProjectSaveAs.Enabled = true;
                    m_BarButtonItemProjectClose.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

    }
}
